package ModelLayer;

import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.time.*;

/**
 * Write a description of class Loan here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Loan
{
    private int id;
    private String borrowDate;
    private String period;
    private boolean state;
    private Person person;
    private Copy copy;

    /**
     * Constructor for objects of class Loan
     */
    public Loan(int id, String borrowDate, String period, Person person, Copy copy)
    {
        this.id = id;
        this.borrowDate = borrowDate;
        this.period = period;
        this.state = true;
        this.person = person;
        this.copy = copy;
    }

    public boolean onTime()
    {
        try
        {
            String dt = this.borrowDate;  
            SimpleDateFormat sdf = new SimpleDateFormat("dddd-MM-yyyy");
            Calendar c = Calendar.getInstance();

            c.setTime(sdf.parse(dt));

            c.add(Calendar.DATE, Integer.parseInt(this.period));  // number of days to add
            dt = sdf.format(c.getTime());  // dt is now the new date

            Date currentDate = new Date();
            Date date = sdf.parse(dt);

            if(date.before(currentDate))
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        catch(Exception e)
        {
            return false;
        }

    }

    //Setters of the loan class.

    public void setPerson(Person person)
    {
        this.person = person;
    }

    public void setCopy(Copy copy)
    {
        this.copy = copy;
    }

    public void setID(int id)
    {
        this.id = id;
    }

    public void setBorrowDate(String borrowDate)
    {
        this.borrowDate = borrowDate;
    }

    public void setPeriod(String period)
    {
        this.period = period;
    }

    public void setState(boolean state)
    {
        this.state = state;
    }

    //Getters of the loan class.

    public Person getPerson()
    {
        return person;
    }

    public Copy getCopy()
    {
        return copy;
    }

    public int getID()
    {
        return id;
    }

    public String getBorrowDate()
    {
        return borrowDate;
    }

    public String getPeriod()
    {
        return period;
    }

    public boolean getState()
    {
        return state;
    }   
}
